﻿using AnyCAD.Foundation;
using AnyCAD.NX.ViewModel;
using CommunityToolkit.Mvvm.Input;
using Rapid.Sketch.Plugin.Command;
using Rapid.Sketch.Plugin.Model;
using Rapid.Sketch.Plugin.Polygon;
using Rapid.Sketch.Plugin.Tag;
using System.Text.Json;
using System.Windows.Input;

namespace Rapid.Sketch.Plugin.ViewModel
{
    internal partial class SketchRibbonViewModel : ViewViewModel
    {

        AnyCAD.Exchange.AsycIpcServer mIpcServer;
        public SketchRibbonViewModel(MainViewModel viewModel)
            : base(viewModel)
        {
            NewMyPolygon = new RelayCommand(OnNewMyPolygon);
            ImportDXF = new RelayCommand(OnImportDXF);
            NewSectionBar = new RelayCommand(OnNewSectionBar);
            PointCloud = new RelayCommand(OnPointCloud);
            AddTag = new RelayCommand(OnAddTag);

            mIpcServer = new AnyCAD.Exchange.AsycIpcServer("pcl.anycad", "anycad.pcl");
        }

        public ICommand PointCloud { get; set; }


        void OnPointCloud()
        {

        }

        public void Unload()
        {
            mIpcServer.Exit();
        }

        public ICommand NewMyPolygon { get; set; }
        void OnNewMyPolygon()
        {

            var model = new MyPolygonModel();
            model.Points.Add(new GPnt(0, 0, 0));
            model.Points.Add(new GPnt(100, 0, 0));
            model.Points.Add(new GPnt(100, 100, 0));
            model.Points.Add(new GPnt(0, 100, 0));

            var undo = new UndoTransaction(Document);
            undo.Start("MyPolygon");
            model.CreateElement(Document);
            undo.Commit();
        }
        public ICommand ImportDXF { get; set; }
        void OnImportDXF()
        {
            var dlg = new Microsoft.Win32.OpenFileDialog
            {
                DefaultExt = ".dxf",
                Filter = "DXF Files (*.dxf)|*.dxf"
            };
            var result = dlg.ShowDialog();
            if (result != true)
                return;
            
            var filename = dlg.FileName;

            var compount = ShapeIO.Open(filename);
            var doc = Document;
            var undo = new UndoTransaction(doc);
            undo.Start("Create");
            //foreach (var shape in shapes)
            {
                var se = ShapeElement.Create(doc);
                se.SetShape(compount);
                se.SetName("Line");
            }
            undo.Commit();

  
        }
        public ICommand NewSectionBar { get; set; }
        void OnNewSectionBar()
        {
            AnyCAD.Foundation.Application.Instance().ExecuteCommand("PICK");

            var doc = Document;

            var undo = new UndoTransaction(doc);
            undo.Start("Tube");
            SectionBarModel.NewInstance(doc, "Bar");
            undo.Commit();
        }

        public ICommand AddTag { get; set; }
        void OnAddTag()
        {
            var doc = Document;

            var undo = new UndoTransaction(doc);
            undo.Start("Tag");
            var store = UserTagModel.CreateInstance("Tag", doc);
            var model = new UserTagModel();
            model.Load(store);
            model.Content = "AnyCAD RapidCAX!";
            model.Position = new GPnt(10, 10, 100);
            model.Save(store);
            undo.Commit();
        }

        [RelayCommand]
        void CopyShape()
        {
            var doc = Document;
            var ids = doc.GetSelection();
            var undo = new UndoTransaction(doc);
            undo.Start("Copy");
            foreach (var id in ids)
            {
                var se = ShapeElement.Cast(doc.FindElement(id));
                if (se == null)
                    continue;

                var newSE = ShapeElement.Create(doc);
                newSE.SetShape(se.GetShape());
            }
            undo.Commit();
        }

        [RelayCommand]
        void AddFollowingPoint()
        {
            MainViewModel.ActiveViewModel.RenderView.SetEditor(new AddFollowingPointEditor());
        }
    }
}
